<?php
class Captcha{
	/**
	 * 背景图片所在目录
	 *
	 * @var string  $folder
	 */
	var $folder     = "";
	var $img_type   = 'png';
	var $width      = 0;
	var $height     = 0;

	/*------------------------------------------------------ */
	//-- 存在session中的名称
	/*------------------------------------------------------ */
	var $session_word = 'captcha_word';

	/**
	 * 背景图片以及背景颜色
	 * @var array   $themes
	 */
	var $themes_jpg = array(
	1 => array('captcha_bg1.jpg', 255, 255, 255),
	2 => array('captcha_bg2.jpg', 255, 255, 255),
	3 => array('captcha_bg3.jpg', 255, 255, 255),
	4 => array('captcha_bg4.jpg', 255, 255, 255),
	5 => array('captcha_bg5.jpg', 255, 255, 255),
	6 => array('captcha_bg6.jpg', 255, 255, 255),
	7 => array('captcha_bg7.jpg', 255, 255, 255),
	8 => array('captcha_bg8.jpg', 255, 255, 255),
	9 => array('captcha_bg9.jpg', 255, 255, 255),
	10 => array('captcha_bg10.jpg', 255, 255, 255),
	);

	/**
	 * 构造函数
	 *
	 * @access  public
	 * @param
	 *
	 * @return void
	 */
	function __construct()
	{
		@session_start();
		$CI =& get_instance();
		$this->folder 	=$CI->config->item("acc_files_path").'/captcha/';
		$this->width	=$CI->config->item("captcha_img_width");
		$this->height	=$CI->config->item("captcha_img_height");
		
		/* 检查是否支持 GD */
		if (PHP_VERSION >= '4.3')
		{
			return (function_exists('imagecreatetruecolor') || function_exists('imagecreate'));
		}
		else
		{
			return (((imagetypes() & IMG_GIF) > 0) || ((imagetypes() & IMG_JPG)) > 0 );
		}
	}


	/**
	 * 检查给出的验证码是否和session中的一致
	 *
	 * @access  public
	 * @param   string  $word   验证码
	 * @return  bool
	 */
	function check_word($word)
	{
		$recorded = isset($_SESSION[$this->session_word]) ? base64_decode($_SESSION[$this->session_word]) : '';
		
		$given    = $this->encrypts_word(strtoupper($word));
		
		return (preg_match("/$given/", $recorded));
	}

	/**
	 * 生成图片并输出到浏览器
	 *
	 * @access  public
	 * @param   string  $word   验证码
	 * @return  mix
	 */
	function generate_image(){
		$word = $this->generate_word();
		/* 记录验证码到session */
		$this->record_word($word);
		$_SESSION['word']=$word;
		/* 验证码长度 */
		$letters = strlen($word);

		/* 选择一个随机的方案 */
		mt_srand((double) microtime() * 1000000);

			
		$theme  = $this->themes_jpg[mt_rand(1, count($this->themes_jpg))];


		if (!file_exists($this->folder . $theme[0]))
		{

			return false;
		}
		else
		{
			$img_bg    = (function_exists('imagecreatefromjpeg') && ((imagetypes() & IMG_JPG) > 0)) ?
			imagecreatefromjpeg($this->folder . $theme[0]) : imagecreatefromgif($this->folder . $theme[0]);
			$bg_width  = imagesx($img_bg);
			$bg_height = imagesy($img_bg);

			$img_org   = ((function_exists('imagecreatetruecolor')) && PHP_VERSION >= '4.3') ?
			imagecreatetruecolor($this->width, $this->height) : imagecreate($this->width, $this->height);

			/* 将背景图象复制原始图象并调整大小 */
			if (function_exists('imagecopyresampled') && PHP_VERSION >= '4.3') // GD 2.x
			{
				imagecopyresampled($img_org, $img_bg, 0, 0, 0, 0, $this->width, $this->height, $bg_width, $bg_height);
			}
			else // GD 1.x
			{
				imagecopyresized($img_org, $img_bg, 0, 0, 0, 0, $this->width, $this->height, $bg_width, $bg_height);
			}
			imagedestroy($img_bg);
			$clr = imagecolorallocate($img_org, $theme[1], $theme[2], $theme[3]);

			/* 绘制边框 */
			//imagerectangle($img_org, 0, 0, $this->width - 1, $this->height - 1, $clr);

			/* 获得验证码的高度和宽度 */
			$x = ($this->width - (imagefontwidth(5) * $letters)) /2;
			$y = ($this->height - imagefontheight(5)) / 2;

			$x_size=rand(3,$this->height - imagefontheight(5)/7-5);

			for($i=0;$i<4;$i++){
				$y_size =rand($y-2,$y+2);
				imagestring($img_org, 5, $x_size, $y_size, substr($word,$i,1), $clr);
				$x_size+=$this->height - imagefontheight(5)/2;
			}


			header('Expires: Thu, 01 Jan 1970 00:00:00 GMT');

			// HTTP/1.1
			header('Cache-Control: private, no-store, no-cache, must-revalidate');
			header('Cache-Control: post-check=0, pre-check=0, max-age=0', false);

			// HTTP/1.0
			header('Pragma: no-cache');
			if ($this->img_type == 'jpeg' && function_exists('imagecreatefromjpeg'))
			{
				header('Content-type: image/jpeg');
				imageinterlace($img_org, 1);
				imagejpeg($img_org, false, 95);
			}
			else
			{
				header('Content-type: image/png');
				imagepng($img_org);
			}

			imagedestroy($img_org);

			return true;
		}
	}

	/*------------------------------------------------------ */
	//-- PRIVATE METHODs
	/*------------------------------------------------------ */

	/**
	 * 对需要记录的串进行加密
	 *
	 * @access  private
	 * @param   string  $word   原始字符串
	 * @return  string
	 */
	function encrypts_word($word)
	{
		return substr(md5($word), 1, 10);
	}

	/**
	 * 将验证码保存到session
	 *
	 * @access  private
	 * @param   string  $word   原始字符串
	 * @return  void
	 */
	function record_word($word)
	{

		$_SESSION[$this->session_word] = base64_encode($this->encrypts_word($word));
	}

	/**
	 * 生成随机的验证码
	 *
	 * @access  private
	 * @param   integer $length     验证码长度
	 * @return  string
	 */
	function generate_word($length = 4)
	{
		$chars = '123456789ABCDEFGHJKLMNPQRSTUVWXYZ';

		for ($i = 0, $count = strlen($chars); $i < $count; $i++)
		{
			$arr[$i] = $chars[$i];
		}
		mt_srand((double) microtime() * 1000000);
		shuffle($arr);
		return substr(implode('', $arr), 5, $length);
	}
}
?>